home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / graphic / jpegsrc4.zip / JCSAMPLE.C < prev    next >
C/C++ Source or Header  |  1992-11-05  |  17KB  |  461 lines

  1. /*
  2.  * jcsample.c
  3.  *
  4.  * Copyright (C) 1991, 1992, Thomas G. Lane.
  5.  * This file is part of the Independent JPEG Group's software.
  6.  * For conditions of distribution and use, see the accompanying README file.
  7.  *
  8.  * This file contains downsampling routines.
  9.  * These routines are invoked via the downsample and
  10.  * downsample_init/term methods.
  11.  *
  12.  * An excellent reference for image resampling is
  13.  *   Digital Image Warping, George Wolberg, 1990.
  14.  *   Pub. by IEEE Computer Society Press, Los Alamitos, CA. ISBN 0-8186-8944-7.
  15.  *
  16.  * The downsampling algorithm used here is a simple average of the source
  17.  * pixels covered by the output pixel.  The hi-falutin sampling literature
  18.  * refers to this as a "box filter".  In general the characteristics of a box
  19.  * filter are not very good, but for the specific cases we normally use (1:1
  20.  * and 2:1 ratios) the box is equivalent to a "triangle filter" which is not
  21.  * nearly so bad.  If you intend to use other sampling ratios, you'd be well
  22.  * advised to improve this code.
  23.  *
  24.  * A simple input-smoothing capability is provided.  This is mainly intended
  25.  * for cleaning up color-dithered GIF input files (if you find it inadequate,
  26.  * we suggest using an external filtering program such as pnmconvol).  When
  27.  * enabled, each input pixel P is replaced by a weighted sum of itself and its
  28.  * eight neighbors.  P's weight is 1-8*SF and each neighbor's weight is SF,
  29.  * where SF = (smoothing_factor / 1024).
  30.  * Currently, smoothing is only supported for 2h2v sampling factors.
  31.  */
  32.  
  33. #include "jinclude.h"
  34.  
  35.  
  36. /*
  37.  * Initialize for downsampling a scan.
  38.  */
  39.  
  40. METHODDEF void
  41. downsample_init (compress_info_ptr cinfo)
  42. {
  43.   /* no work for now */
  44. }
  45.  
  46.  
  47. /*
  48.  * Downsample pixel values of a single component.
  49.  * This version handles arbitrary integral sampling ratios, without smoothing.
  50.  * Note that this version is not actually used for customary sampling ratios.
  51.  */
  52.  
  53. METHODDEF void
  54. int_downsample (compress_info_ptr cinfo, int which_component,
  55.         long input_cols, int input_rows,
  56.         long output_cols, int output_rows,
  57.         JSAMPARRAY above, JSAMPARRAY input_data, JSAMPARRAY below,
  58.         JSAMPARRAY output_data)
  59. {
  60.   jpeg_component_info * compptr = cinfo->cur_comp_info[which_component];
  61.   int inrow, outrow, h_expand, v_expand, numpix, numpix2, h, v;
  62.   long outcol, outcol_h;    /* outcol_h == outcol*h_expand */
  63.   JSAMPROW inptr, outptr;
  64.   INT32 outvalue;
  65.  
  66. #ifdef DEBUG            /* for debugging pipeline controller */
  67.   if (output_rows != compptr->v_samp_factor ||
  68.       input_rows != cinfo->max_v_samp_factor ||
  69.       (output_cols % compptr->h_samp_factor) != 0 ||
  70.       (input_cols % cinfo->max_h_samp_factor) != 0 ||
  71.       input_cols*compptr->h_samp_factor != output_cols*cinfo->max_h_samp_factor)
  72.     ERREXIT(cinfo->emethods, "Bogus downsample parameters");
  73. #endif
  74.  
  75.   h_expand = cinfo->max_h_samp_factor / compptr->h_samp_factor;
  76.   v_expand = cinfo->max_v_samp_factor / compptr->v_samp_factor;
  77.   numpix = h_expand * v_expand;
  78.   numpix2 = numpix/2;
  79.  
  80.   inrow = 0;
  81.   for (outrow = 0; outrow < output_rows; outrow++) {
  82.     outptr = output_data[outrow];
  83.     for (outcol = 0, outcol_h = 0; outcol < output_cols;
  84.      outcol++, outcol_h += h_expand) {
  85.       outvalue = 0;
  86.       for (v = 0; v < v_expand; v++) {
  87.     inptr = input_data[inrow+v] + outcol_h;
  88.     for (h = 0; h < h_expand; h++) {
  89.       outvalue += (INT32) GETJSAMPLE(*inptr++);
  90.     }
  91.       }
  92.       *outptr++ = (JSAMPLE) ((outvalue + numpix2) / numpix);
  93.     }
  94.     inrow += v_expand;
  95.   }
  96. }
  97.  
  98.  
  99. /*
  100.  * Downsample pixel values of a single component.
  101.  * This version handles the common case of 2:1 horizontal and 1:1 vertical,
  102.  * without smoothing.
  103.  */
  104.  
  105. METHODDEF void
  106. h2v1_downsample (compress_info_ptr cinfo, int which_component,
  107.          long input_cols, int input_rows,
  108.          long output_cols, int output_rows,
  109.          JSAMPARRAY above, JSAMPARRAY input_data, JSAMPARRAY below,
  110.          JSAMPARRAY output_data)
  111. {
  112.   int outrow;
  113.   long outcol;
  114.   register JSAMPROW inptr, outptr;
  115.  
  116. #ifdef DEBUG            /* for debugging pipeline controller */
  117.   jpeg_component_info * compptr = cinfo->cur_comp_info[which_component];
  118.   if (output_rows != compptr->v_samp_factor ||
  119.       input_rows != cinfo->max_v_samp_factor ||
  120.       (output_cols % compptr->h_samp_factor) != 0 ||
  121.       (input_cols % cinfo->max_h_samp_factor) != 0 ||
  122.       input_cols*compptr->h_samp_factor != output_cols*cinfo->max_h_samp_factor)
  123.     ERREXIT(cinfo->emethods, "Bogus downsample parameters");
  124. #endif
  125.  
  126.   for (outrow = 0; outrow < output_rows; outrow++) {
  127.     outptr = output_data[outrow];
  128.     inptr = input_data[outrow];
  129.     for (outcol = 0; outcol < output_cols; outcol++) {
  130.       *outptr++ = (JSAMPLE) ((GETJSAMPLE(*inptr) + GETJSAMPLE(inptr[1])
  131.                   + 1) >> 1);
  132.       inptr += 2;
  133.     }
  134.   }
  135. }
  136.  
  137.  
  138. /*
  139.  * Downsample pixel values of a single component.
  140.  * This version handles the standard case of 2:1 horizontal and 2:1 vertical,
  141.  * without smoothing.
  142.  */
  143.  
  144. METHODDEF void
  145. h2v2_downsample (compress_info_ptr cinfo, int which_component,
  146.          long input_cols, int input_rows,
  147.          long output_cols, int output_rows,
  148.          JSAMPARRAY above, JSAMPARRAY input_data, JSAMPARRAY below,
  149.          JSAMPARRAY output_data)
  150. {
  151.   int inrow, outrow;
  152.   long outcol;
  153.   register JSAMPROW inptr0, inptr1, outptr;
  154.  
  155. #ifdef DEBUG            /* for debugging pipeline controller */
  156.   jpeg_component_info * compptr = cinfo->cur_comp_info[which_component];
  157.   if (output_rows != compptr->v_samp_factor ||
  158.       input_rows != cinfo->max_v_samp_factor ||
  159.       (output_cols % compptr->h_samp_factor) != 0 ||
  160.       (input_cols % cinfo->max_h_samp_factor) != 0 ||
  161.       input_cols*compptr->h_samp_factor != output_cols*cinfo->max_h_samp_factor)
  162.     ERREXIT(cinfo->emethods, "Bogus downsample parameters");
  163. #endif
  164.  
  165.   inrow = 0;
  166.   for (outrow = 0; outrow < output_rows; outrow++) {
  167.     outptr = output_data[outrow];
  168.     inptr0 = input_data[inrow];
  169.     inptr1 = input_data[inrow+1];
  170.     for (outcol = 0; outcol < output_cols; outcol++) {
  171.       *outptr++ = (JSAMPLE) ((GETJSAMPLE(*inptr0) + GETJSAMPLE(inptr0[1]) +
  172.                   GETJSAMPLE(*inptr1) + GETJSAMPLE(inptr1[1])
  173.                   + 2) >> 2);
  174.       inptr0 += 2; inptr1 += 2;
  175.     }
  176.     inrow += 2;
  177.   }
  178. }
  179.  
  180.  
  181. /*
  182.  * Downsample pixel values of a single component.
  183.  * This version handles the special case of a full-size component,
  184.  * without smoothing.
  185.  */
  186.  
  187. METHODDEF void
  188. fullsize_downsample (compress_info_ptr cinfo, int which_component,
  189.              long input_cols, int input_rows,
  190.              long output_cols, int output_rows,
  191.              JSAMPARRAY above, JSAMPARRAY input_data, JSAMPARRAY below,
  192.              JSAMPARRAY output_data)
  193. {
  194. #ifdef DEBUG            /* for debugging pipeline controller */
  195.   if (input_cols != output_cols || input_rows != output_rows)
  196.     ERREXIT(cinfo->emethods, "Pipeline controller messed up");
  197. #endif
  198.  
  199.   jcopy_sample_rows(input_data, 0, output_data, 0, output_rows, output_cols);
  200. }
  201.  
  202.  
  203. #ifdef INPUT_SMOOTHING_SUPPORTED
  204.  
  205. /*
  206.  * Downsample pixel values of a single component.
  207.  * This version handles the standard case of 2:1 horizontal and 2:1 vertical,
  208.  * with smoothing.
  209.  */
  210.  
  211. METHODDEF void
  212. h2v2_smooth_downsample (compress_info_ptr cinfo, int which_component,
  213.             long input_cols, int input_rows,
  214.             long output_cols, int output_rows,
  215.             JSAMPARRAY above, JSAMPARRAY input_data, JSAMPARRAY below,
  216.             JSAMPARRAY output_data)
  217. {
  218.   int inrow, outrow;
  219.   long colctr;
  220.   register JSAMPROW inptr0, inptr1, above_ptr, below_ptr, outptr;
  221.   INT32 membersum, neighsum, memberscale, neighscale;
  222.  
  223. #ifdef DEBUG            /* for debugging pipeline controller */
  224.   jpeg_component_info * compptr = cinfo->cur_comp_info[which_component];
  225.   if (output_rows != compptr->v_samp_factor ||
  226.       input_rows != cinfo->max_v_samp_factor ||
  227.       (output_cols % compptr->h_samp_factor) != 0 ||
  228.       (input_cols % cinfo->max_h_samp_factor) != 0 ||
  229.       input_cols*compptr->h_samp_factor != output_cols*cinfo->max_h_samp_factor)
  230.     ERREXIT(cinfo->emethods, "Bogus downsample parameters");
  231. #endif
  232.  
  233.   /* We don't bother to form the individual "smoothed" input pixel values;
  234.    * we can directly compute the output which i